# encoding: utf-8

class Comment
  def initialize(basic_hash, comment_info, type)
    @driver = basic_hash['driver']
    @comment = comment_info
    @type = type
  end

  def add_comment
    @driver.find_element(:id, "new_note_textarea").send_keys @comment['content']
    @driver.find_element(:xpath, "//input[@value='增加评论']").click
  end

  def update_comment
    @driver.find_element(:xpath, "//a[@class='btn btn-mini comment_edit_btn']").click
    case @type
      when 'merge'
        @driver.find_element(:id, "textarea").send_keys @comment['update_content']
        @driver.find_element(:id, "new_note_form_submit").click
      when 'issue'
        @driver.find_element(:id, "note_issue_note").send_keys @comment['update_content']
        @driver.find_element(:xpath, "/html/body/div[2]/div[3]/div/div/div/dl/dd/div[2]/form/div[2]/input").click
    end
  end

  def del_comment
    case @type
      when 'merge'
        url = @driver.find_element(:id, "notes-list").find_element(:link, "删除").attribute("href")
        id_num = url.index("#") + 1
        id = url[id_num, url.length]
        @driver.find_element(:id, "notes-list").find_element(:link, "删除").click
        @driver.find_element(:id, "#{id}").find_element(:tag_name, "a").click
      when 'issue'
        @driver.find_element(:id, "note_issues_table").find_element(:link, "删除").click
        close_alert_and_get_its_text
    end
  end
end